// i.d(t, { m1: () => PinCollider, Vp: () => PinMesh })
import * as THREE from "three"
import r from "../shader/76570"
import a from "../shader/69254"

class PinMaterial extends THREE.RawShaderMaterial {
  static uniforms: {
    tMask: { type: string; value: any }
    tPinHole: { type: string; value: any }
    pinColor: { type: string; value: THREE.Color }
    opacity: { type: string; value: number }
  }
  constructor(e, t) {
    const i = THREE.UniformsUtils.clone(PinMaterial.uniforms)
    i.tMask.value = e
    i.tPinHole.value = t
    super({
      vertexShader: r,
      fragmentShader: a,
      uniforms: i,
      name: "PinMaterial",
      transparent: !0
    })
  }
}
PinMaterial.uniforms = {
  tMask: { type: "t", value: null },
  tPinHole: { type: "t", value: null },
  pinColor: { type: "c", value: new THREE.Color() },
  opacity: { type: "f", value: 1 }
}
import TextureLoader from "../loader/TextureLoader"
import Color from "../three/Color"
import TypedMesh from "./TypedMesh"
import PinCollider from "./PinCollider"
import renderOrderEnum from "../enum/renderOrder.enum"
import funcUtil from "../utils/func.util"
import CameraData from "../data/CameraData"

let m
const g = () => m || ((m = TextureLoader("./images/360_placement_pin_mask.png")), m)
export default class PinMesh extends TypedMesh<THREE.PlaneBufferGeometry, PinMaterial> {
  static geometry: THREE.PlaneBufferGeometry
  cameraData: CameraData
  _collider: PinCollider
  static colliderGeometry: THREE.BufferGeometry
  static colliderMaterial: THREE.Material
  opacityProgress: number
  shouldHide: boolean
  uniforms: { [uniform: string]: THREE.IUniform<any> }
  selected: number
  static FADE_DURATION: number
  constructor(e, t, i, n) {
    const s = new PinMaterial(g(), t)
    super(PinMesh.geometry, s)
    this.layers.mask = n.mask
    this.name = "PinMesh"
    this.cameraData = i
    this._collider = new PinCollider(PinMesh.colliderGeometry, PinMesh.colliderMaterial)
    this._collider.material.visible = !1
    this.add(this._collider)
    this.opacityProgress = 0
    this.shouldHide = !0
    this.uniforms = s.uniforms
    this.unhover()
    this.selected = 0
    this.visible = !1
    this.position.copy(e)
    this.renderOrder = renderOrderEnum.pins360
  }
  updatePosition(e) {
    this.position.copy(e)
  }
  get collider() {
    return this._collider
  }
  render(e) {
    this.quaternion.copy(this.cameraData.pose.rotation)
    null === this.uniforms.tPinHole.value
      ? (this.opacityProgress = 0)
      : !this.shouldHide && this.opacityProgress < 1
      ? (this.opacityProgress += e / PinMesh.FADE_DURATION)
      : this.shouldHide && this.opacityProgress > 0 && (this.opacityProgress -= e / PinMesh.FADE_DURATION)
    this.opacityProgress = funcUtil.getMax(this.opacityProgress, 0, 1)
    this.uniforms.opacity.value = this.opacityProgress
    this.visible = 0 !== this.opacityProgress
  }
  activate() {
    this.shouldHide = !1
  }
  deactivate() {
    this.shouldHide = !0
  }
  dispose() {
    this.collider.geometry.dispose(), this.collider.material.dispose()
  }
  setPinHover(e, t = !0, i = !0) {
    const n = Math.min(Math.max(e, 0), 1)
    this.selected !== n &&
      (i && this.uniforms.pinColor.value.copy(Color.WHITE).lerp(Color.MP_BRAND, n),
      t && n > 0 && n <= 1 && (this.material.depthTest = !1),
      0 === n && (this.material.depthTest = !0),
      (this.selected = n))
  }
  unhover() {
    this.uniforms.pinColor.value.copy(Color.WHITE)
  }
}
PinMesh.FADE_DURATION = 500
PinMesh.geometry = new THREE.PlaneBufferGeometry(1.5, 1.5)
PinMesh.colliderGeometry = new THREE.SphereBufferGeometry(1.5 * 0.65)
PinMesh.colliderMaterial = new THREE.MeshBasicMaterial({
  transparent: !0,
  opacity: 0.5,
  depthWrite: !1,
  color: 16724312
})
